﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PBOffice.DAL;
using PBOffice.BLL;

public partial class PublicInformationModule_phoneBook : System.Web.UI.Page
{
    List<Position> posList = new List<Position>();
    PositionManager pm = new PositionManager();
    UserManager um = new UserManager();
    List<v_UserDepPosition> userList = new List<v_UserDepPosition>();
    int currentPageIndex;         //当前页面编号
    int pageSize = 3;   //页面显示的记录数
    protected void Page_Load(object sender, EventArgs e)
    {
        //页面首次加载
        if(!IsPostBack)
        {
            lblNone.Visible = false;
            userList = um.ShowAllUserInfo();       //获取所有用户信息
            currentPageIndex = 1;            //当前页面编号
            ShowPageUsers(currentPageIndex);    //绑定第一页用户信息 
            anpPhone.RecordCount = userList.Count;       //符合条件的用户个数 
            if (anpPhone.RecordCount == 0)
                lblNone.Visible = true;
            lblCurrentPageIndex.Text = "1";     //当前页面：第一页
            lblPageCount.Text = anpPhone.PageCount.ToString();   //页面总数
        }        
    }
    //显示指定页面用户信息
    private void ShowPageUsers(int currentPageIndex)
    {
        int pageIndex = currentPageIndex - 1;    //当前页面索引
        List<v_UserDepPosition> userPage = new List<v_UserDepPosition>();        
        anpPhone.RecordCount = userList.Count;       //符合条件的用户个数 
        int usersLastPageCount = anpPhone.RecordCount - pageIndex * pageSize;    //最后一页的用户信息记录条数
        if (usersLastPageCount < pageSize)
        {
            for (int i = pageIndex * pageSize; i < anpPhone.RecordCount; i++)
            {
                userPage.Add(userList[i]);
            }
        }
        else
        {
            for (int i = (pageIndex * pageSize) ; i <(pageIndex * pageSize) + pageSize; i++)
            {
                userPage.Add(userList[i]);
            }
        }
        gvPhoneBook.DataSource = userPage;     //绑定用户信息
        gvPhoneBook.DataBind();        
            
    }
    /// <summary>
    /// 搜索按钮单击事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ibtnSearch_Click(object sender, ImageClickEventArgs e)
    {
        v_UserDepPosition user = new v_UserDepPosition();
        user.DepId = txtDepId.Text.Trim();    //部门名称
        user.PosNo =int.Parse( ddlPositon.SelectedValue.ToString());  //职位编号
        user.UserName = txtUserName.Text.Trim();          //用户姓名
        userList = um.ShowUserInfoByUserInfo(user);       //根据部门民称、职位编号、姓名模糊查询用户信息
        currentPageIndex = 1;     //加载符合条件的第一页用户信息
        ShowPageUsers(currentPageIndex);
    }
    //页面变化事件
    protected void anpPhone_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
    {
        currentPageIndex = e.NewPageIndex;    //当前页面索引
        lblCurrentPageIndex.Text = currentPageIndex.ToString();    //当前页面索引
        userList = um.ShowAllUserInfo();       //获取所有用户信息
        ShowPageUsers(currentPageIndex);      //显示当前索引页面用户信息
    }
}